/*!
 * VERSION: 0.2.0
 * DATE: 2016-10-20
 * GIT: https://github.com/shrekshrek/orienter
 * @author: Shrek.wang
 **/
var Orienter;
(function (factory) {

  if (typeof define === 'function' && define.amd) {
    define(['exports'], function (exports) {
      Orienter = factory(exports);
    });
  } else if (typeof exports !== 'undefined') {
    factory(exports);
  } else {
    Orienter = factory({});
  }

}(function (Orienter) {

  Orienter = function () {
    this.initialize.apply(this, arguments);
  };

  Orienter.prototype = {
    lon: 0,
    lat: 0,
    direction: 0,
    fix: 0,
    os: '',
    initialize: function (config) {
      var _config = config || {};

      this.onOrient = _config.onOrient || function () { };
      this.onChange = _config.onChange || function () { };

      this._orient = this._orient.bind(this);
      /* this._change = this._change.bind(this); */

      this.lon = 0;
      this.lat = 0;
      this.direction = 0; // 纵屏

      switch (this.direction) {
        case 0:
          this.fix = 0;
          break;
        case 90:
          this.fix = -270;
          break;
        case -90:
          this.fix = -90;
          break;
      }
    },

    /* init: function () {
      window.addEventListener('deviceorientation', this._orient, false);
      window.addEventListener('orientationchange', this._change, false);
    },

    destroy: function () {
      window.removeEventListener('deviceorientation', this._orient, false);
      window.removeEventListener('orientationchange', this._change, false);
    },

    _change: function (event) {
      this.direction = window.orientation;

      this.onChange(this.direction);
    }, */

    changeDirectionTo: function (n) {
      this.direction = n;
    },

    _orient: function (event) {
      switch (this.os) {
        case 'ios':
          switch (this.direction) {
            case 0:
              this.lon = event.alpha + event.gamma;
              if (event.beta > 0) this.lat = event.beta - 90;
              break;
            case 90:
              if (event.gamma < 0) {
                this.lon = event.alpha - 90;
              } else {
                this.lon = event.alpha - 270;
              }
              if (event.gamma > 0) {
                this.lat = 90 - event.gamma;
              } else {
                this.lat = -90 - event.gamma;
              }
              break;
            case -90:
              if (event.gamma < 0) {
                this.lon = event.alpha - 90;
              } else {
                this.lon = event.alpha - 270;
              }
              if (event.gamma < 0) {
                this.lat = 90 + event.gamma;
              } else {
                this.lat = -90 + event.gamma;
              }
              break;
          }
          break;
        case 'android':
          switch (this.direction) {
            case 0:
              this.lon = event.alpha + event.gamma + 30;
              if (event.gamma > 90) {
                this.lat = 90 - event.beta;
              } else {
                this.lat = event.beta - 90;
              }
              break;
            case 90:
              this.lon = event.alpha - 230;
              if (event.gamma > 0) {
                this.lat = 270 - event.gamma;
              } else {
                this.lat = -90 - event.gamma;
              }
              break;
            case -90:
              this.lon = event.alpha - 180;
              this.lat = -90 + event.gamma;
              break;
          }
          break;
      }

      this.lon += this.fix;
      this.lon %= 360;
      if (this.lon < 0) this.lon += 360;

      this.lon = Math.round(this.lon);
      this.lat = Math.round(this.lat);

      this.onOrient({
        a: Math.round(event.alpha),
        b: Math.round(event.beta),
        g: Math.round(event.gamma),
        lon: this.lon,
        lat: this.lat,
        dir: this.direction
      });
    }

  };

  // return Orienter;
  module.exports = {
    Orienter: Orienter
  }
}));
